package com.zhenhunfan.hello.rpc.demo02;

import com.zhenhunfan.hello.rpc.common.User;

import java.io.*;
import java.net.Socket;

/**
 * <p>Title: Sub</p>
 * <p>Description: 客户端网络请求的代理</p>
 *
 * @author zhenhunfan
 * @version 1.0.0
 * @date 2021/4/8
 */
public class Stub {
    public User findUserById(int id) throws IOException {
        Socket socket = new Socket("localhost", 9000);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        dos.writeInt(123);

        OutputStream out = socket.getOutputStream();
        out.write(baos.toByteArray());
        out.flush();

        DataInputStream dis = new DataInputStream(socket.getInputStream());
        int i = dis.readInt();
        String name = dis.readUTF();
        User user = new User(i,name);


        dos.close();
        socket.close();
        return user;
    }
}
